package com.rainng.coursesystem.manager.admin;

import com.rainng.coursesystem.dao.ResourceDAO;
import com.rainng.coursesystem.dao.MajorDAO;
import com.rainng.coursesystem.dao.TeacherDAO;
import com.rainng.coursesystem.manager.BaseManager;
import com.rainng.coursesystem.model.entity.ResourceEntity;
import com.rainng.coursesystem.model.vo.response.IdNameVO;

import com.rainng.coursesystem.model.vo.response.table.ResourceItemVO;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Component
public class ResourceManager extends BaseManager {
    private final ResourceDAO resourceDAO;
    private final TeacherDAO teacherDAO;
    private final MajorDAO majorDAO;

    public ResourceManager(ResourceDAO resourceDAO, TeacherDAO teacherDAO, MajorDAO majorDAO) {
        this.resourceDAO = resourceDAO;
        this.teacherDAO = teacherDAO;
        this.majorDAO = majorDAO;
    }

    public Integer getPageCount(String name) {
        int count = resourceDAO.count(name);
        return calcPageCount(count, ResourceDAO.PAGE_SIZE);
    }

    @Transactional
    public List<ResourceItemVO> getPage(Integer index, String namePart) {
        List<ResourceItemVO> resourceItemList = new ArrayList<>();
        List<ResourceEntity> resourceList = resourceDAO.getPage(index, namePart);

        for (ResourceEntity resource : resourceList) {
            int id = resource.getId();
            String name = resource.getName();
            String remarks = resource.getRemarks();
            resourceItemList.add(new ResourceItemVO(id, name,remarks));
        }

        return resourceItemList;
    }

    public ResourceEntity get(Integer id) {
        return resourceDAO.get(id);
    }

    public int create(ResourceEntity entity) {
        return resourceDAO.insert(entity);
    }

    public int update(ResourceEntity entity) {
        return resourceDAO.update(entity);
    }

    public int delete(Integer id) {
        return resourceDAO.delete(id);
    }



    public List<IdNameVO> listName() {
        List<IdNameVO> voList = new ArrayList<>();
        List<ResourceEntity> entityList = resourceDAO.listName();
        for (ResourceEntity entity : entityList) {
            voList.add(new IdNameVO(entity.getId(), entity.getName()));
        }

        return voList;
    }
}
